import 'dart:async';

import 'package:cjybase/util/event.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'base_page.dart';

abstract class BasePageState<T extends BasePage> extends State<T> {
  StreamSubscription _subscription;

  @override
  void initState() {
    //订阅eventbus
    _subscription = Event.eventBus.on<Event>().listen((event) {
      eventListen(event);
    });
    initData();
    super.initState();
  }

  eventListen(Event event) {}

  /// 初始化数据
  initData() async {}

  @override
  void dispose() {
    _subscription.cancel();
    loadingDismiss();
    destroyData();
    super.dispose();
  }

  /// 销毁数据
  destroyData() async {}

  loadingShow({String status}) {
    if (status == null)
      EasyLoading.show();
    else
      EasyLoading.show(status: status);
  }

  loadingDismiss() {
    EasyLoading.dismiss();
  }

  @override
  Widget build(BuildContext context);

  /// 跳转界面 A->B
  push(BuildContext context, BasePage page) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (_) {
          return page;
        },
      ),
    );
  }

  /// 跳转界面 A->B,A界面销毁
  pushReplacement(BuildContext context, BasePage page) {
    Navigator.pushReplacement(
      context,
      MaterialPageRoute(
        builder: (_) {
          return page;
        },
      ),
    );
  }

  Widget appBar({
    String title = "appBar",
    List<Color> titleColors,
    Widget view,
    Function viewClick,
    Function backClick,
  }) {
    List<Color> colors = [];
    if (titleColors != null) {
      colors.addAll(titleColors);
    }
    if (colors.isEmpty) {
      colors.add(Color.fromRGBO(82, 96, 230, 1));
      colors.add(Color.fromRGBO(63, 169, 255, 1));
    }
    if (colors.length == 1) {
      colors.add(titleColors[0]);
    }
    return Column(
      children: [
        Container(
          padding: EdgeInsets.only(top: 34.h),
          height: 100.h,
          alignment: Alignment.center,
          decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: colors,
              begin: Alignment.centerLeft,
              end: Alignment.centerRight,
            ),
          ),
          child: Stack(
            alignment: Alignment.center,
            children: [
              Center(
                child: Text(
                  '$title',
                  style: TextStyle(fontSize: 20.sp, color: Colors.white),
                ),
              ),
              Positioned(
                child: Icon(
                  Icons.chevron_left,
                  color: Colors.white,
                  size: 30,
                ),
                left: 10.w,
              ),
              Positioned(
                child: InkWell(
                  onTap: () {
                    if (backClick == null) {
                      Navigator.pop(context);
                    } else {
                      backClick();
                    }
                  },
                  child: Container(
                    height: 64.h,
                    width: 80.w,
                  ),
                ),
                left: 0.w,
              ),
              view == null
                  ? Container()
                  : Positioned(
                      child: InkWell(
                        onTap: viewClick,
                        child: Container(
                          height: 66.h,
                          child: view,
                        ),
                      ),
                      right: 10.w,
                    ),
            ],
          ),
        ),
      ],
    );
  }
}
